<!-- 我的订单 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="打款状态" prop="status">
        <el-select v-model="queryParams.remitStates" placeholder="打款状态" clearable style="width: 240px">
          <el-option label="未打款" value='0' />
          <el-option label="已打款" value='1' />
          <el-option label="订单失效" value='2' />
        </el-select>
      </el-form-item>
      <el-form-item label="打款时间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="remitList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="打款编号" align="center" prop="billId" />
      <el-table-column label="打款状态" align="center" prop="remitStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.remitStatus == '0'">未打款</span>
          <span v-else="scope.row.remitStatus=='1'">已打款</span>
          <span v-else="scope.row.remitStatus=='2'">订单失效</span>
        </template>
      </el-table-column>
      <el-table-column label="打款金额" align="center" prop="remitMoney" />
      <el-table-column label="打款人" align="center" prop="remitPeople" />
      <el-table-column label="打款时间" align="center" prop="remitTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.remitTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.remitStatus == 0">
          <el-button type="text" icon="el-icon-edit" @click="remittance(scope.row)">打款</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 订单详情 -->
    <el-dialog :visible.sync="open" width="1000px" append-to-body>
      <el-descriptions title="订单详情" :visible.sync="open" append-to-body direction="vertical" :column="4" border>
        <template slot="extra">
          <el-button type="primary" size="small" :disabled="affirmDisabled" @click="affirmRemittance()">确认打款
          </el-button>
        </template>
        <el-descriptions-item label="用户姓名">{{ orderInfo.realName }}</el-descriptions-item>
        <el-descriptions-item label="产品名称" :span="2">{{ orderInfo.productName }}</el-descriptions-item>
        <el-descriptions-item label="申请金额">{{ orderInfo.applymoney }}</el-descriptions-item>
        <el-descriptions-item label="还款方式">
          <label v-if="orderInfo.paymentMethod == 0" >等额本息</label>
          <label v-else-if="orderInfo.paymentMethod == 1">等额本金</label>
        </el-descriptions-item>
        <el-descriptions-item label="审核人">{{ orderInfo.auditor }}</el-descriptions-item>
        <el-descriptions-item label="审核状态">
          <label v-if="orderInfo.auditStatus == 0" style="color: blue;">未审核</label>
          <label v-else-if="orderInfo.auditStatus == 1" style="color: green;">通过</label>
          <label v-else=" orderInfo.auditStatus==2" style="color: red;">未通过</label>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" :page-sizes="[5, 10, 15, 20]" />
  </div>
</template>
  <script>
  import { getOrder } from '@/api/credit/creOrder';
  import { getRemitList, remitOrderType } from '@/api/credit/creRemit'
  export default {
    name: "Order",
    data() {
      return {
        // 日期范围
        dateRange: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 表格数据
        remitList: [],
        // 订单数据
        orderInfo: {},
        // 还款表id
        remitId: '',
        // 是否显示弹出层
        open: false,
        affirmDisabled: true,
        dialogVisible: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 5,
          remitStates: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
    },
    watch: {
      orderInfo: function () {
        if (this.orderInfo.auditStatus === '1') {
          this.affirmDisabled = false
        } else {
          this.affirmDisabled = true
        }
      }
    },
    methods: {
      //折叠面板方法
      handleChange(val) {
  
      },
      /** 查询【请填写功能名称】列表 */
      getList() {
        this.loading = true;
        getRemitList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.remitList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          pageNum: 1,
          pageSize: 5,
          productName: null,
          auditStatus: null,
        };
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.reset();
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.orderId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('credit/order/export', {
          ...this.queryParams
        }, `order_${new Date().getTime()}.xlsx`)
      },
      /* 打款 */
      remittance(row) {
        this.remitId = row.billId;
        this.loading = true;
        getOrder(row.orderId).then(res => {
          this.orderInfo = res.data;
        }).catch();
        this.loading = false;
        this.open = true;
      },
      // 确认打款
      affirmRemittance() {
        let orderId = this.orderInfo.orderId
        this.$confirm('是否确定给' + this.orderInfo.realName + '发放贷款', '发放贷款', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          remitOrderType(orderId, this.remitId).then(res => {
            this.getList();
            this.open = false;
            this.$message({
              type: 'success',
              message: '打款成功!',
            });
          }).catch();
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消打款'
          });
        });
      }
    }
  };
  </script>
  